<?php
require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_coupon.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_discount_type.php";
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_sql_convert.php';


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of api_security
 *
 * @author Thien
 */
class api_coupons {
    
    public static function save_coupon($coupon_id, $coupon_code, $discount_type_id, $discount_number) {
        $dao_coupon = new dao_coupon();
        return $dao_coupon->save($coupon_id, $coupon_code, $discount_type_id, $discount_number);
    }
    
    public static function get_coupon_by_coupon_code($coupon_code) {
        $dao_coupon = new dao_coupon();
        return $dao_coupon->get_id_by_coupon_code($coupon_code);
    }
    
    public static function delete_coupon($coupon_id) {
        $dao_coupon = new dao_coupon();
        return $dao_coupon->delete($coupon_id);
    }
    
    public static function get_coupon_by_id($coupon_id) {
        $dao_coupon = new dao_coupon();
        return $dao_coupon->get_by_id($coupon_id);
    }
    
    public static function get_all_coupons() {
        $dao_coupon = new dao_coupon();
        return $dao_coupon->get_all();
    }
    
    public static function get_all_discount_types() {
        $dao_discount_type = new dao_discount_type();
        return $dao_discount_type->get_all();
    }
    
    public static function get_all_coupons_with_now_showing() {
        $dao_coupon = new dao_coupon();
        $status_id = 2; //for now showing
        return $dao_coupon->get_all_by_status_id($status_id);
    }
    
    public static function get_all_coupons_with_comming_soon() {
        $dao_coupon = new dao_coupon();
        $status_id = 1; //for comming soon
        return $dao_coupon->get_all_by_status_id($status_id);
    }
    
    public static function get_all_coupon_statuses() {
        $dao_coupon_status = new dao_coupon_status();
        return $dao_coupon_status->get_all();
    }
    
    public static function get_default_discount_type() {
        $dao_discount_type = new dao_discount_type();
        $coupon = $dao_discount_type->get_first();
        if (!empty($coupon['name'])) {
            return $coupon['name'];
        }
        return "";
    }
    
    public static function get_discount_type_id_from_name($name) {
        $dao_discount_type = new dao_discount_type();
        $discount_type = $dao_discount_type->get_id_by_name($name);
        return $discount_type['id'];
    }
    
    public static function validate_coupon_fields($coupon_code, $description, $discount_type) {
        $error_show = "";
        if (empty($coupon_code)) {
            $error_show .= "<li>Please fill Name.</li>";
        }
        if (empty($description)) {
            $error_show .= "<li>Please fill Description.</li>";
        }
        if (empty($discount_type)) {
            $error_show .= "<li>Please choose a Discount Type.</li>";
        }
        return $error_show;
    }
    
}
